; (function () {
  new Vue({
    el: '#business-config',
    data: {
      channelTypeList: [],
      channelNameList: [],
      allChannelNameList: [],
      isShowChannelTypeIpt: false,
      isShowChannelNameIpt: false,
      addTypeValue: null,
      addNameValue: null,
      parentCode: null,
      tmpName: null,  // 用于编辑时缓存名称
      module_code: 'CSTM_JWK',
      config_name: 'CSTM_JWK_REGION_CITY',
      config_name1: 'CSTM_JWK_REGION_DISTRICT',
    },
    directives: {
      focus: {
        inserted: function (el) {
          el.focus()
        },
        update: function (el) {
          el.focus()
        }
      }
    },
    methods: {
      getChannelType() {
        var self = this;
        hlhttp.get({
          url: 'configure/dict',
          data: { 'module_code': this.module_code, 'config_name': this.config_name },
          success: data => {
            data.forEach(el => {
              el['isEdit'] = 'N'
              el['isSelect'] = 'N'
            })
            self.channelTypeList = data;
          }
        })
      },
      getChannelName() {
        var self = this;
        hlhttp.get({
          url: 'configure/dict',
          data: { 'module_code': this.module_code, 'config_name': this.config_name1 },
          success: data => {
            data.forEach(el => el['isEdit'] = 'N')
            self.allChannelNameList = data;
          }
        })
      },
      selectChannelType(name, code, i) {
        this.parentCode = code;
        this.channelNameList = this.allChannelNameList.filter(el => el.parentDictCode == code);
        this.channelTypeList.forEach(el => el['isSelect'] = 'N')
        this.$set(this.channelTypeList[i], 'isSelect', 'Y')
      },
      addType() {
        this.addTypeValue = null;
        this.isShowChannelTypeIpt = true;
      },
      addChannel() {
        let flag = false
        this.channelTypeList.some(item =>{
          if(item.isSelect == 'Y'){
            flag = true
          }
        })
        if(!flag){
          hl.toast('请选择城市')
          return
        }
        this.addNameValue = null;
        this.isShowChannelNameIpt = true;
      },
      editChannelType(i) {
        this.tmpName = this.channelTypeList[i]['dictName']
        this.$set(this.channelTypeList[i], 'isEdit', 'Y')
      },
      delChannelType(code, i) {
        var self = this;
        hlhttp.delete({
          url: 'configure/merchant_channel?module_code=' + this.module_code + '&&config_name=' + this.config_name + '&&dict_code=' + code,
          success: data => {
            self.channelTypeList.splice(i, 1);
            hl.toast('删除成功')
          }
        })
      },
      editChannelName(i) {
        this.$set(this.channelNameList[i], 'isEdit', 'Y')
      },
      delChannelName(code, i) {
        var self = this;
        hlhttp.delete({
          url: 'configure/merchant_channel?module_code=' + this.module_code + '&&config_name=' + this.config_name1 + '&&dict_code=' + code,
          success: data => {
            self.getChannelName()
            self.channelNameList.splice(i, 1);
            hl.toast('删除成功')
          }
        })
      },
      hideChannelTypeIpt() {
        this.isShowChannelTypeIpt = false;
      },
      hideChannelNameIpt() {
        this.isShowChannelNameIpt = false;
      },
      confirmAddChannelType() {
        var self = this;
        if (!self.addTypeValue) {
          hl.toast('城市不能为空');
          return
        }

        hlhttp.post({
          url: 'configure/merchant_channel',
          data: { 'module_code': this.module_code, 'config_name': this.config_name, 'parent_code': '#', 'value': self.addTypeValue },
          success: data => {
            self.channelTypeList.push({ 'dictCode': data.dictCode, 'dictName': self.addTypeValue, 'isEdit': 'N', 'isSelect': 'N' })
            this.selectChannelType(self.addTypeValue, data.dictCode, self.channelTypeList.length - 1)
            self.isShowChannelTypeIpt = false;
            hl.toast('新增成功')
          }
        })
      },
      cancelAddChannelType() {
        this.isShowChannelTypeIpt = false;
      },
      confirmAddChannelName() {
        var self = this;
        if (!self.addNameValue) {
          hl.toast('区县不能为空');
          return
        }

        hlhttp.post({
          url: 'configure/merchant_channel',
          data: { 'module_code': this.module_code, 'config_name': this.config_name1, 'parent_code': self.parentCode, 'value': self.addNameValue },
          success: data => {
            self.getChannelName()
            self.channelNameList.push({ 'dictCode': data.dictCode, 'dictName': self.addNameValue, 'parentDictCode': self.parentCode, 'isEdit': 'N' })
            self.isShowChannelNameIpt = false;
            hl.toast('新增成功')
          }
        })
      },
      cancelAddChannelName() {
        this.isShowChannelNameIpt = false;
      },
      confirmEditChannelType(name, code, i) {
        var self = this;
        hlhttp.put({
          url: 'configure/merchant_channel',
          data: { 'module_code': this.module_code, 'config_name': this.config_name, 'dict_code': code, 'value': name },
          success: data => {
            self.$set(this.channelTypeList[i], 'isEdit', 'N')
            hl.toast('编辑成功')
          }
        })
      },
      cancelEditChannelType(i) {
        this.$set(this.channelTypeList[i], 'dictName', this.tmpName)
        this.$set(this.channelTypeList[i], 'isEdit', 'N')
      },
      confirmEditChannelName(name, code, i) {
        var self = this;
        hlhttp.put({
          url: 'configure/merchant_channel',
          data: { 'module_code': this.module_code, 'config_name': this.config_name1, 'dict_code': code, 'value': name },
          success: data => {
            self.$set(self.channelNameList[i], 'isEdit', 'N')
            hl.toast('编辑成功')
          }
        })
      },
      cancelEditChannelName(i) {
        this.$set(this.channelNameList[i], 'isEdit', 'N')
      }
    },
    created() {
      this.getChannelType()
      this.getChannelName()
    },
    mounted() {

    }
  })

}())
